

#ifndef __ZUC_INTER_COMM_H__
#define __ZUC_INTER_COMM_H__

#define ZUC_INTER_CAN_MSG_TYPE_STD (0)
#define ZUC_INTER_CAN_MSG_TYPE_EXT (1)

#define ZUC_INTER_CAN_MSG_DIR_RX (0)
#define ZUC_INTER_CAN_MSG_DIR_TX (1)
#define CABNUM (2)
#define ISCABV3 (1)

#define SCB 0
#define PSCB 'P'
#define TMS570 1
#define STM407 2

#define SCB_CAN_RX_MAX_CNT (100)
#define SCB_CAN_TX_MAX_CNT (8)
#define SCB_CAN_STA_INIT (1)
#define SCB_CAN_STA_PREOP (2)
#define SCB_CAN_STA_SAFEOP (4)  //仅接收不发送
#define SCB_CAN_STA_OP (8)      //即接收也发送
#define SCB_RANDOM_INDEX_TIMEOUT 100

/*
 *BP:
 * RX:0x191
 * TX:0x211
 *SCB:
 * RX:0x192,
 *    0x292,
 *    0x298,
 *    0x592
 * TX:0x212,
 *    0x312
 *    0x612
*/
typedef enum
{

    /*Rx messages*/
    ZUC_INTER_CAN_0x191 = 0,
    ZUC_INTER_CAN_0x192,
    ZUC_INTER_CAN_0x193,
    ZUC_INTER_CAN_0x292,
    ZUC_INTER_CAN_0x298,
    ZUC_INTER_CAN_0x592,

    /*Tx messages*/
    ZUC_INTER_CAN_0x211,
    ZUC_INTER_CAN_0x212,
    ZUC_INTER_CAN_0x293,
    ZUC_INTER_CAN_0x312,
    ZUC_INTER_CAN_0x612,
    ZUC_INTER_CAN_MSG_ALL
} zuc_inter_can_msg_handle_t;

typedef void (*zuc_inter_can_rx_handler_t)(void);

typedef struct zuc_inter_can_msg_s
{
    unsigned int can_msg_id;
    unsigned int msg_type;
    unsigned char direction;
    unsigned short period;

    unsigned short timeout;
    unsigned char* msg_data;  //用来接收数据
    zuc_inter_can_rx_handler_t rx_handler;
} zuc_inter_can_msg_t;

typedef struct zuc_inter_can_msg_sts_s
{
    unsigned int msg_offline_cnt;
    unsigned int msg_recv_cnt;
    unsigned char msg_offline_sts;
    unsigned int snd_cnt;
    unsigned char tx_en_flag;
} zuc_inter_can_msg_sts_t;

typedef struct zuc_inter_btn_status_s
{
    unsigned char reserve : 2;
    unsigned char lock_remote : 1;
    unsigned char terminate : 1;
    unsigned char run_pause : 1;
    unsigned char return_btn : 1;
    unsigned char robot_en : 1;
    unsigned char power_ctl : 1;

} zuc_inter_btn_status_t;

typedef struct scb_do_signal_s
{
    unsigned char user_do_1 : 1;
    unsigned char user_do_2 : 1;
    unsigned char user_do_3 : 1;
    unsigned char user_do_4 : 1;
    unsigned char user_do_5 : 1;
    unsigned char user_do_6 : 1;
    unsigned char user_do_7 : 1;
    unsigned char user_do_8 : 1;

    unsigned char user_do_9 : 1;
    unsigned char user_do_10 : 1;
    unsigned char user_do_11 : 1;
    unsigned char user_do_12 : 1;
    unsigned char user_do_13 : 1;
    unsigned char user_do_14 : 1;
    unsigned char user_do_15 : 1;
    unsigned char user_do_16 : 1;

    unsigned char local_do_8 : 1;
    unsigned char local_do_pwr_bp : 1;
    unsigned char local_do_req_reflash_bp : 1;
    unsigned char local_do_5 : 1;
    unsigned char local_re_flash : 1;
    unsigned char local_dis_robot_can : 1;
    unsigned char local_pwr_ipc : 1;  // shut down confirm
    unsigned char local_pwr_robot : 1;

    unsigned char local_do_9 : 1;
    unsigned char local_do_10 : 1;
    unsigned char local_do_11 : 1;
    unsigned char local_do_12 : 1;
    unsigned char local_do_13 : 1;
    unsigned char local_do_14 : 1;
    unsigned char local_do_15 : 1;
    unsigned char local_do_16 : 1;
    unsigned char resv_1;
    unsigned char resv_2;
    unsigned char resv_3;
    unsigned char resv_4 : 6;
} scb_do_signal_t;

typedef struct pscb_do_signal_s
{
    unsigned char user_do_1 : 1;
    unsigned char user_do_2 : 1;
    unsigned char user_do_3 : 1;
    unsigned char user_do_4 : 1;
    unsigned char user_do_5 : 1;
    unsigned char user_do_6 : 1;
    unsigned char user_do_7 : 1;
    unsigned char user_do_8 : 1;

    unsigned char user_do_resv : 8;

    unsigned char local_do_8 : 1;
    unsigned char local_do_pwr_bp : 1;
    unsigned char local_do_req_reflash_bp : 1;
    unsigned char local_do_5 : 1;
    unsigned char local_re_flash : 1;
    unsigned char local_dis_robot_can : 1;
    unsigned char local_pwr_ipc : 1;
    unsigned char local_pwr_robot : 1;

    unsigned char local_do_9 : 1;
    unsigned char local_do_10 : 1;
    unsigned char local_do_11 : 1;
    unsigned char local_do_12 : 1;
    unsigned char local_do_13 : 1;
    unsigned char local_do_14 : 1;
    unsigned char local_do_15 : 1;
    unsigned char local_do_16 : 1;

    unsigned char user_do_resv_1;
    unsigned char resv_2;
    unsigned char resv_3;
    unsigned char resv_4;
} pscb_do_signal_t;

typedef union
{
    scb_do_signal_t do_signal;
    pscb_do_signal_t pscb_do_signal;
    unsigned char msg_data[8];
} zuc_scb_do_t;

typedef struct scb_ao_signal_s
{
    unsigned char ao_type;
    unsigned char ao_data_1_h;
    unsigned char ao_data_1_l;
    unsigned char ao_data_2_h;
    unsigned char ao_data_2_l;

    unsigned char resv_2;
    unsigned char resv_3;
    unsigned char resv_4;
} scb_ao_signal_t;

typedef enum
{
    SCB_RGB_OFF,
    SCB_RGB_BLUE,
    SCB_RGB_GREEN,
    SCB_RGB_YELLOW,
    SCB_RGB_RED,

    SCB_RGB_YELLOW_BLINK,
    SCB_RGB_RESV_1,
    SCB_RGB_RESV_2,
    SCB_RGB_BLUE_BLINK,
    SCB_RGB_ALL
} pscb_rgb_color_t;

typedef struct pscb_ao_signal_s
{
    unsigned char user_dio_cfg;
    unsigned char stuck_voltage_cfg;
    pscb_rgb_color_t front_pnal_rgb_ctl;
    unsigned char resv_4;
    unsigned char resv_5;
    unsigned char resv_6;
    unsigned char resv_7;
    unsigned char resv_8;
} pscb_ao_signal_t;

typedef union
{
    scb_ao_signal_t ao_signal;
    pscb_ao_signal_t pscb_ao_signal;
    unsigned char msg_data[8];
} zuc_scb_ao_t;

typedef union
{
    struct
    {
        unsigned char node_index;
        unsigned char ob_index_h;
        unsigned char ob_index_l;
        unsigned char ob_sub_index;

        unsigned char ob_data_1;
        unsigned char ob_data_2;
        unsigned char ob_data_3;
        unsigned char ob_data_4;
        unsigned char ob_data_5;
    } req_signal;
    unsigned char msg_data[8];
} zuc_scb_req_resp_t;

typedef struct scb_safety_do_signal_s
{
    unsigned char safety_do_channel0 : 1;
    unsigned char safety_do_channel1 : 1;
    unsigned char safety_do_channel2 : 1;
    unsigned char safety_do_channel3 : 1;
    unsigned char safety_do_channel4 : 1;
    unsigned char safety_do_channel5 : 1;
    unsigned char safety_do_channel6 : 1;
    unsigned char safety_do_channel7 : 1;

    unsigned char resv_1;
    unsigned char resv_2;
    unsigned char resv_3;
    unsigned char resv_4;
    unsigned char resv_5;
    unsigned char resv_6;
    unsigned char resv_7;
} scb_safety_do_signal_t;
typedef union
{
    scb_safety_do_signal_t safety_do_signal;
    unsigned char msg_data[8];
} zuc_scb_safety_do_ctl_t;

typedef struct scb_safety_di_signal_s
{
    //safety DI (reserved)
    unsigned char safety_di_collision_lv0 : 1;  //0x193,B0-b0
    unsigned char safety_di_collision_lv1 : 1;  //0x193,B0-b1
    unsigned char safety_di_collision_lv2 : 1;  //0x193,B0-b2
    unsigned char safety_di_collision_lv3 : 1;  //0x193,B0-b3
    unsigned char safety_di_collision_lv4 : 1;  //0x193,B0-b4
    unsigned char safety_di_collision_lv5 : 1;  //0x193,B0-b5
    unsigned char safety_di_collision_lv7 : 1;  //0x193,B0-b6
    unsigned char safety_di_7 : 1;
    //reserved
    unsigned char reserved_1;
    /*safety DO feedback from SCB*/
    unsigned char feedback_sfdo_channel0 : 1;
    unsigned char feedback_sfdo_channel1 : 1;
    unsigned char feedback_sfdo_channel2 : 1;
    unsigned char feedback_sfdo_channel3 : 1;
    unsigned char feedback_sfdo_channel4 : 1;
    unsigned char feedback_sfdo_channel5 : 1;
    unsigned char feedback_sfdo_channel6 : 1;
    unsigned char feedback_sfdo_channel7 : 1;

    unsigned char reserved_3;
    unsigned char reserved_4;
    unsigned char reserved_5;
    unsigned char reserved_6;
    unsigned char reserved_7;
} scb_safety_io_signal_t;

typedef union
{
    //
    scb_safety_io_signal_t safety_io_signal;
    unsigned char msg_data[8];
} zuc_scb_safety_io_t;

typedef struct scb_di_signal_s
{
    unsigned char user_di_1 : 1;
    unsigned char user_di_2 : 1;
    unsigned char user_di_3 : 1;
    unsigned char user_di_4 : 1;
    unsigned char user_di_5 : 1;
    unsigned char user_di_6 : 1;
    unsigned char user_di_7 : 1;
    unsigned char user_di_8 : 1;

    unsigned char user_di_9 : 1;
    unsigned char user_di_10 : 1;
    unsigned char user_di_11 : 1;
    unsigned char user_di_12 : 1;
    unsigned char user_di_13 : 1;
    unsigned char user_di_14 : 1;
    unsigned char user_di_15 : 1;
    unsigned char user_di_16 : 1;

    unsigned char local_di_8 : 1;
    unsigned char local_pwr_off_req : 1;  // request
    unsigned char local_usr_stop_2 : 1;
    unsigned char local_usr_stop_1 : 1;
    unsigned char local_usr_estop_2 : 1;
    unsigned char local_usr_estop_1 : 1;
    unsigned char local_bp_estop_2 : 1;
    unsigned char local_bp_estop_1 : 1;

    unsigned char local_di_9 : 1;
    unsigned char local_di_10 : 1;
    unsigned char local_mode_sel : 1;
    unsigned char local_three_position_enabling : 1;
    unsigned char local_sfgrd_rst : 1;
    unsigned char local_reduce : 1;
    unsigned char local_add_sfgrd_stop : 1;
    unsigned char local_add_estop : 1;

    unsigned char io_type_bit0 : 1;
    unsigned char io_type_bit1 : 1;
    unsigned char io_type_bit2 : 1;
    unsigned char io_type_bit3 : 1;
    unsigned char reserved : 4;
    unsigned char uhi_speed_hl;
    unsigned char uhi_speed_lh;
    unsigned char uhi_speed_ll;
} scb_di_signal_t;
typedef struct pscb_di_signal_s
{
    unsigned char user_di_1 : 1;
    unsigned char user_di_2 : 1;
    unsigned char user_di_3 : 1;
    unsigned char user_di_4 : 1;
    unsigned char user_di_5 : 1;
    unsigned char user_di_6 : 1;
    unsigned char user_di_7 : 1;
    unsigned char user_di_8 : 1;

    unsigned char user_di_resv;

    unsigned char local_di_8 : 1;
    unsigned char local_pwr_off_req : 1;
    unsigned char local_usr_stop_2 : 1;
    unsigned char local_usr_stop_1 : 1;
    unsigned char local_usr_estop_2 : 1;
    unsigned char local_usr_estop_1 : 1;
    unsigned char local_bp_estop_2 : 1;
    unsigned char local_bp_estop_1 : 1;

    unsigned char local_di_9 : 1;
    unsigned char local_di_10 : 1;
    unsigned char local_mode_sel : 1;
    unsigned char local_three_position_enabling : 1;
    unsigned char local_sfgrd_rst : 1;
    unsigned char local_reduce : 1;
    unsigned char local_add_sfgrd_stop : 1;
    unsigned char local_add_estop : 1;

    unsigned char resv_1;
    unsigned char resv_2;
    unsigned char resv_3;
    unsigned char resv_4;
} pscb_di_signal_t;
typedef union
{
    scb_di_signal_t di_signal;
    pscb_di_signal_t pscb_di_signal;
    unsigned char msg_data[8];
} zuc_scb_di_t;

typedef struct scb_ai_signal_s
{
    unsigned char ai_type;
    unsigned char ai_data_1_h;
    unsigned char ai_data_1_l;
    unsigned char ai_data_2_h;
    unsigned char ai_data_2_l;

    unsigned char uhi_pules_direction;
    unsigned char uhi_pules_h;
    unsigned char uhi_pules_l;
} scb_ai_signal_t;

typedef struct pscb_ai_signal_s
{
    unsigned char dio_type;
    unsigned char stuck_voltage;
    unsigned char rgb_status;
    unsigned char resv_4;
    unsigned char resv_5;
    unsigned char resv_6;
    unsigned char resv_7;
    unsigned char resv_8;
} pscb_ai_signal_t;

typedef union
{
    scb_ai_signal_t ai_signal;
    pscb_ai_signal_t pscb_ai_signal;
    unsigned char msg_data[8];
} zuc_scb_ai_t;

typedef struct scb_do_ao_feedback_s
{
    unsigned char user_do_1 : 1;
    unsigned char user_do_2 : 1;
    unsigned char user_do_3 : 1;
    unsigned char user_do_4 : 1;
    unsigned char user_do_5 : 1;
    unsigned char user_do_6 : 1;
    unsigned char user_do_7 : 1;
    unsigned char user_do_8 : 1;

    unsigned char user_do_9 : 1;
    unsigned char user_do_10 : 1;
    unsigned char user_do_11 : 1;
    unsigned char user_do_12 : 1;
    unsigned char user_do_13 : 1;
    unsigned char user_do_14 : 1;
    unsigned char user_do_15 : 1;
    unsigned char user_do_16 : 1;

    unsigned char local_do_1 : 1;
    unsigned char local_do_2 : 1;
    unsigned char local_do_3 : 1;
    unsigned char local_do_4 : 1;
    unsigned char local_do_5 : 1;
    unsigned char local_do_6 : 1;
    unsigned char local_do_7 : 1;
    unsigned char local_do_8 : 1;

    unsigned char local_do_9 : 1;
    unsigned char local_do_10 : 1;
    unsigned char local_do_11 : 1;
    unsigned char local_do_12 : 1;
    unsigned char local_do_13 : 1;
    unsigned char local_do_14 : 1;
    unsigned char local_do_15 : 1;
    unsigned char local_do_16 : 1;

    unsigned char io_type_bit0 : 1;
    unsigned char io_type_bit1 : 1;
    unsigned char io_type_bit2 : 1;
    unsigned char io_type_bit3 : 1;
    unsigned char ao_data_1_h : 4;
    unsigned char ao_data_1_l;
    unsigned char ao_data_2_h;
    unsigned char ao_data_2_l;
} scb_do_ao_feedback_t;

typedef struct pscb_do_feedback_s
{
    unsigned char user_do_1 : 1;
    unsigned char user_do_2 : 1;
    unsigned char user_do_3 : 1;
    unsigned char user_do_4 : 1;
    unsigned char user_do_5 : 1;
    unsigned char user_do_6 : 1;
    unsigned char user_do_7 : 1;
    unsigned char user_do_8 : 1;

    unsigned char user_resv;
    unsigned char user_resv_1;
    unsigned char user_resv_2;
    unsigned char user_resv_3;
    unsigned char user_resv_4;
    unsigned char user_resv_5;
    unsigned char user_resv_6;
} pscb_do_feedback_t;
typedef struct zuc_set_safety_configuration_req
{
    int safetyDI_param;
    int safetyDO_param;
    int robot_power_limit_param;
    int drag_tcp_speed_limit_param;

} zuc_set_safety_configuration_req;

typedef struct zuc_set_common_configuration_req
{
    int RC_version_param;
    int TCP_offset_param;
    int cab_voltage_param;
    int cab_id_param;

} zuc_set_common_configuration_req;

typedef union
{
    scb_do_ao_feedback_t do_ao_feedback;
    pscb_do_feedback_t pscb_do_feedback;
    unsigned char msg_data[8];
} zuc_scb_do_ao_feedback_t;

typedef union uint_float_u
{
    unsigned int uint_data;
    float float_data;
} uint_float_t;

typedef struct zuccab_internal_info_s
{
    /*scb information*/
    /*status information*/
    /**
	 * @brief scb_di_uhi_info is the union of :
	 * 	
	 * 	SCB-->	scb_di_signal_t di_signal;
		PSCB-->	pscb_di_signal_t pscb_di_signal;
		CAN-->	unsigned char msg_data[8];
	 * 
	 */
    zuc_scb_di_t scb_di_uhi_info;

    /**
	 * @brief ai is SCB only
	 * 
	 */
    zuc_scb_ai_t scb_ai_uhi_info;
    zuc_scb_do_ao_feedback_t scb_do_ao_feedback_info;

    /*control information*/
    /**	scb_do_ctl:
	 * 	SCB-->  	scb_do_signal_t do_signal;
		PSCB--> 	pscb_do_signal_t pscb_do_signal;
		CAN--> 		unsigned char msg_data[8]; 
	*/
    zuc_scb_do_t scb_do_ctl;

    /**
	 * @brief 
	 * for SCB AO functions
	 * for PSCB:
	 * 			DIO CFG
	 * 			stuck voltage CFG
	 * 			front panel control
	 */
    zuc_scb_ao_t scb_ao_ctl;

    //实现控制器主动触发安全状态后安全DO也能正确输出
    zuc_scb_safety_do_ctl_t scb_safety_do_ctl;

    /* Byte 0:为以后的安全DI预留
     * Byte 2:8个安全DO输出通道状态反馈
     * 其他皆为预留
    */
    zuc_scb_safety_io_t scb_safety_io_info;

    /**
	 * @brief sw_release_sts: release status
	 * 	high 8bits: 'P' or 0 indicate that the device is PSCB or SCB
	 *   when 'P':
	 * 		UHI is not supported
	 * 		Analog is not support
	 * 		local user stop is not support,default 1
	 * 		
	 * 	low 8 bits: 'D' or 'R' indicate that the firmware is in debug or release status
	 */
    int sw_release_sts;
    /**
	 * @brief 
	 * 	"P":80d:0x50h:-->PSCB
	 *              0:-->SCB
	 */
    int p_scb_type;

    unsigned int cabinet_id;

    /*bp information*/
    /**
	 * @brief
	 * 	button control information
	 *  power on/off robot
	 *  enable/disable robot
	 * 	lock/unlock
	 * 	run/terminate
	 *  pause/resume
	 *  ...
	 */
    // zuc_button_panel_cmd_t button_panel_cmd;
    // /*control information
    // * lock rgb is controled by internal can module
    // * logol rgb is controled by motion module
    // */
    // zuc_button_panel_control_t button_panel_control;
    /*control the robot return to init position 
	* the robot is in init position :1
	* the robot is returnning init position: 0
	* this is used to control the lock rgb light
	*/
    int rtrn_init_pos_done;

    /*connection status
	* one message lost will be connection fault<==>offline 
	**/
    int scb_offline_sts;
    int bp_offline_sts;
    int inter_can_bsof_sts;

    /**
     * request set configuration to SCB
    */
    zuc_set_safety_configuration_req set_safety_config_req;
    zuc_set_common_configuration_req set_common_config_req;

    /**
	 * data for SCB (0x612)
	 */
    unsigned int sf_input_cfg_out;   //输入安全DI类型设置
    unsigned int sf_output_cfg_out;  //输入安全DO类型设置
    uint_float_t robot_power_limit;  //机器人整机功率报警阈值，float类型，数据格式按照IEEE754标准传输，单位W
    uint_float_t tcp_offset_x;       //TCP Offset X，float类型，数据格式按照IEEE754标准传输，单位mm
    uint_float_t tcp_offset_y;       //TCP Offset Y，float类型，数据格式按照IEEE754标准传输，单位mm
    uint_float_t tcp_offset_z;       //TCP Offset Z，float类型，数据格式按照IEEE754标准传输，单位mm
    unsigned int cmdCabVoltage;      //设置CAB ID
    unsigned int drag_tcp_speed_limit;
    unsigned int rc_version;
    /**
	 * data from SCB (0x592)
	 */
    unsigned int sf_input_cfg_fb;   //输入安全DI配置
    unsigned int sf_output_cfg_fb;  //输出安全DO配置
    int robot_power;                //机器人功率
    int robot_voltage;              //机器人电压
    int robot_current;              //机器人电流
    int user_io_current;            //用户IO电流
    int pwr_24v_v;                  //24V电压采样
    int fan_current;                //风扇电流
    /**
     * 硬件版本号规则：
        数据长度：16bit
        示例：'A''B'，AB均为字符,ASCII码形式进行数据传输，硬件版本号中A当前可用于区分硬件平台，A为'1'表示570平台，'2'表示407平台
    */
    int hw_version;              //硬件版本号
    int sw_major_version;        //软件版本号大版本号
    int sw_minor_version;        //软件版本号小版本号
    int pdu_temperature;         //PDU温度
    unsigned int curr_run_time;  //本次工控机供电时间（单位：s）
    unsigned int accu_run_time;  //工控机累计供电时间（单位：s）
    unsigned int cabinet_id_fb;  //控制柜序列号
    unsigned int curCabVoltage;  //控制柜供电电压（单位：V）

} zucab_internal_info_t;
//
// for motion module reference
extern zucab_internal_info_t cab_internal_info;

extern void set_scb_safety_DI_param(unsigned int safety_di_cfg);
extern void set_scb_safety_DO_param(unsigned int safety_do_cfg);
extern void set_scb_power_limit_param(float power_limit);
extern void set_scb_TCP_offset_param(double x, double y, double z);
extern void set_scb_cab_id_param(unsigned int cabinet_id);
extern void set_scb_cab_voltage_param(unsigned int cab_voltage);
extern void set_scb_drag_tcp_speed_limit_param(unsigned int drag_tcp_speed_limit);

#endif